home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gnat1792.zip / gnat179b / t-adainc / i-c.adb < prev    next >
Text File  |  1994-05-19  |  7KB  |  246 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                         I N T E R F A C E S . C                          --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.1 $                              --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. --
  22. --                                                                          --
  23. ------------------------------------------------------------------------------
  24.  
  25. package body Interfaces.C is
  26.  
  27.    ------------
  28.    -- To_Ada --
  29.    ------------
  30.  
  31.    --  Convert Char_Array to String (function form)
  32.  
  33.    function To_Ada
  34.      (Item     : in Char_Array;
  35.       Trim_Nul : in Boolean := True)
  36.       return     String
  37.    is
  38.       Result : String (1 .. Item'Length);
  39.  
  40.    begin
  41.       for J in Item'range loop
  42.          if Item (J) = Nul and then Trim_Nul then
  43.             return Result (1 .. J - Item'First + Result'First - 1);
  44.          else
  45.             Result (J - Item'First + Result'First) := C_To_Ada (Item (J));
  46.          end if;
  47.       end loop;
  48.  
  49.       if Trim_Nul then
  50.          raise Unterminated;
  51.       end if;
  52.  
  53.       return Result;
  54.    end To_Ada;
  55.  
  56.    --  Convert Char_Array to String (procedure form)
  57.  
  58.    procedure To_Ada
  59.      (Item       : in  Char_Array;
  60.       Target     : out String;
  61.       Last       : out Natural;
  62.       Trim_Nul   : in Boolean := True)
  63.    is
  64.    begin
  65.       Last := 0;
  66.  
  67.       for J in Item'range loop
  68.          if Item (J) = Nul and then Trim_Nul then
  69.             return;
  70.          end if;
  71.  
  72.          Last := Last + 1;
  73.          Target (Last) := C_To_Ada (Item (J));
  74.       end loop;
  75.  
  76.       if Trim_Nul then
  77.          raise Unterminated;
  78.       end if;
  79.    end To_Ada;
  80.  
  81.    --  Convert WChar_T to Wide_Character
  82.  
  83.    function To_Ada (Item : in WChar_T) return Wide_Character is
  84.    begin
  85.       return Wide_Character (Item);
  86.    end To_Ada;
  87.  
  88.    --  Convert Wide_Char_Array to Wide_String (function form)
  89.  
  90.    function To_Ada
  91.      (Item        : in  Wide_Char_Array;
  92.       Trim_Nul    : in  Boolean := True)
  93.       return        Wide_String
  94.    is
  95.       Result : Wide_String (1 .. Item'Length);
  96.  
  97.    begin
  98.       for J in Item'range loop
  99.          if Item (J) = Wide_Nul and then Trim_Nul then
  100.             return Result (1 .. J - Item'First + Result'First - 1);
  101.          else
  102.             Result (J - Item'First + Result'First) :=
  103.               Wide_Character (Item (J));
  104.          end if;
  105.       end loop;
  106.  
  107.       if Trim_Nul then
  108.          raise Unterminated;
  109.       end if;
  110.  
  111.       return Result;
  112.    end To_Ada;
  113.  
  114.    --  Convert Wide_Char_Array to Wide_String (procedure form)
  115.  
  116.    procedure To_Ada
  117.      (Item       : in  Wide_Char_Array;
  118.       Target     : out Wide_String;
  119.       Last       : out Natural;
  120.       Trim_Nul   : in  Boolean := True)
  121.    is
  122.    begin
  123.       Last := 0;
  124.  
  125.       for J in Item'range loop
  126.          if Item (J) = Wide_Nul and then Trim_Nul then
  127.             return;
  128.          end if;
  129.  
  130.          Last := Last + 1;
  131.          Target (Last) := Wide_Character (Item (J));
  132.       end loop;
  133.  
  134.       if Trim_Nul then
  135.          raise Unterminated;
  136.       end if;
  137.    end To_Ada;
  138.  
  139.    ----------
  140.    -- To_C --
  141.    ----------
  142.  
  143.    --  Convert String to Char_Array (function form)
  144.  
  145.    function To_C
  146.      (Item       : in String;
  147.       Append_Nul : in Boolean := True)
  148.       return       Char_Array
  149.    is
  150.       Result : Char_Array (0 .. Item'Length - Boolean'Pos (not Append_Nul));
  151.  
  152.    begin
  153.       for J in Item'range loop
  154.          Result (J - Item'First) := Ada_To_C (Item (J));
  155.       end loop;
  156.  
  157.       if Append_Nul then
  158.          Result (Item'Length) := Nul;
  159.       end if;
  160.  
  161.       return Result;
  162.    end To_C;
  163.  
  164.    --  Convert String to Char_Array (procedure form)
  165.  
  166.    --  Note: in the following procedure, we are relying on the built in
  167.    --  constraint checking to propagate Constraint_Error when required,
  168.    --  so checks must be on if this checking is required.
  169.    
  170.    procedure To_C
  171.      (Item       : in  String;
  172.       Target     : out Char_Array;
  173.       Last       : out Integer;
  174.       Append_Nul : in  Boolean := True)
  175.    is
  176.    begin
  177.       Last := -1;
  178.  
  179.       for J in Item'range loop
  180.          Last          := Last + 1;
  181.          Target (Last) := Ada_To_C (Item (J));
  182.       end loop;
  183.  
  184.       if Append_Nul then
  185.          Last          := Last + 1;
  186.          Target (Last) := Nul;
  187.       end if;
  188.    end To_C;
  189.  
  190.    --  Convert Wide_Character to Wchar_T
  191.    
  192.    function To_C (Item : in Wide_Character) return WChar_T is
  193.    begin
  194.       return WChar_T (Item);
  195.    end To_C;
  196.  
  197.    --  Convert Wide_String to Wide_Char_Array (function form)
  198.    
  199.    function To_C
  200.      (Item        : in  Wide_String;
  201.       Append_Nul  : in  Boolean := True)
  202.       return        Wide_Char_Array
  203.    is
  204.       Result :
  205.         Wide_Char_Array (0 .. Item'Length - Boolean'Pos (not Append_Nul));
  206.  
  207.    begin
  208.       for J in Item'range loop
  209.          Result (J - Item'First) := WChar_T (Item (J));
  210.       end loop;
  211.  
  212.       if Append_Nul then
  213.          Result (Item'Length) := Wide_Nul;
  214.       end if;
  215.  
  216.       return Result;
  217.    end To_C;
  218.    
  219.    --  Convert Wide_String to Wide_Char_Array (procedure form)
  220.  
  221.    --  Note: in the following procedure, we are relying on the built in
  222.    --  constraint checking to propagate Constraint_Error when required,
  223.    --  so checks must be on if this checking is required.
  224.  
  225.    procedure To_C
  226.      (Item       : in  Wide_String;
  227.       Target     : out Wide_Char_Array;
  228.       Last       : out Integer;
  229.       Append_nul : in  Boolean := True)
  230.    is
  231.    begin
  232.       Last := -1;
  233.  
  234.       for J in Item'range loop
  235.          Last          := Last + 1;
  236.          Target (Last) := WChar_T (Item (J));
  237.       end loop;
  238.  
  239.       if Append_Nul then
  240.          Last          := Last + 1;
  241.          Target (Last) := Wide_Nul;
  242.       end if;
  243.    end To_C;
  244.  
  245. end Interfaces.C;
  246.